﻿using Dynamics.NAV.Metro.Common;
using Dynamics.NAV.Metro.Data;
using Okra.Core;
using Okra.Navigation;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Dynamics.NAV.Metro.Pages.VendorDetail
{
    public class VendorDetailViewModel : BindableBase, IActivatable<NavVendor, string>
    {
        private NavVendor _selectedVendor;
        private IList<NavVendor> _Vendors;

        public VendorDetailViewModel()
        {
            this.GoBackCommand = new DelegateCommand(GoBack);

            NavVendorDataSource navDataSource = new NavVendorDataSource();
            this.Vendors = navDataSource.GetVendors();
        }

        [ImportingConstructor]
        public VendorDetailViewModel(INavVendorDataSource navVendorDataSource)
        {
            this.GoBackCommand = new DelegateCommand(GoBack);
            this.Vendors = navVendorDataSource.GetVendors();
        }

        
        [Import]
        public INavigationManager NavigationManager { get; set; }

        public ICommand GoBackCommand { get; private set; }

        public bool CanGoBack
        {
            get { return NavigationManager.CanGoBack; }
        }

        public void GoBack()
        {
            NavigationManager.GoBack();
        }

        public void Activate(NavVendor arguments, string state)
        {
            if (arguments == null)
                this.SelectedVendor = this.Vendors.FirstOrDefault();
            else
                this.SelectedVendor = arguments;
        }

        public string SaveState()
        {
            return null;
        }

        public NavVendor SelectedVendor
        {
            get 
            {
                if (this._selectedVendor == null)
                {
                    this.SelectedVendor = this.Vendors.FirstOrDefault();
                }

                return this._selectedVendor; 
            }
            set
            {
                if (_selectedVendor != value)
                {
                    _selectedVendor = value;
                    OnPropertyChanged("SelectedVendor");
                }
            }
        }

        public IList<NavVendor> Vendors
        {
            get
            {
                return _Vendors;
            }
            set
            {
                if (_Vendors != value)
                {
                    _Vendors = value;
                    OnPropertyChanged("Vendors");
                }
            }
        }
    }
}
